package com.bkhc.common.util.response;

import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.List;

public class PageUtil {
    //当前页码
    private Integer pageNum = 1;
    //每页显示条数
    private Integer pageSize = 5;
    //当前条件下的总记录数
    private Integer total = 0;
    //总页数
    private Integer totalPage = 1;
    //开始行号
    private Integer startNum = 0;
    //查询的结果集
    private List<?> list = new ArrayList<>();
    public Integer getPageNo() {
        return pageNum;
    }
    public  void setPageNo(Integer pageNum) {
        this .pageNum = pageNum;
    }
    public Integer getPageSize() {
        return pageSize;
    }
    public  void setPageSize(Integer pageSize) {
        this .pageSize = pageSize;
    }
    public int getTotal() {
      return total;
    }
    public  void setTotal (Integer total) {
        this .total = total;
    }
    public Integer getTotalPage() {
        //总页数
        totalPage = total / pageSize;
        if (total == 0 || total% pageSize!= 0 ) {
            totalPage ++ ;
        }
        return totalPage;
    }
    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }
    public Integer getStartNum() {
        //开始行号=(当前页数-1)x每页显示的条数
        return (pageNum - 1) * pageSize;
    }
    public void setStartNum(Integer startNum) {
        this.startNum = startNum;
    }
    public List<?> getList() {
        return list;
    }
    public void setList(List<?> list) {
        this .list = list;
    }
    public PageUtil(){}

    public PageUtil(Page page){
        this.setList(page.getContent());
        this.setPageNo(page.getPageable().getPageNumber());
        this.setTotal(Long.valueOf(page.getTotalElements()).intValue());
        this.setPageSize(page.getPageable().getPageSize());
        this.setStartNum(Long.valueOf(page.getPageable().getOffset()).intValue());
    }
}
